package com.fenghen.demo03.example;

import java.util.Arrays;
import java.util.List;

public class StreamTest {
    public void tradition(){
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
        // 传统for循环
        for (String name : names) {
            if (name.startsWith("A")) {
                System.out.println(name.toUpperCase());
            }
        }
    }

    public void  stream(){
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
        // 使用Stream替代for循环
        names.stream()
                .filter(name -> name.startsWith("A"))
                .map(String::toUpperCase)
                .forEach(System.out::println); // 输出: ALICE
    }

    public static void main(String[] args) {
        StreamTest streamTest = new StreamTest();
//        streamTest.tradition();
        streamTest.stream();
    }

}
